import {createOrder,payOrder,payStatus} from '../../api/api'
Page({
  data: {
    address:'',
    cartList:[],
    priceAll:0,
    numAll:0
  },
  async payGoods(){
    let {address}=this.data
    let addr=address.provinceName+address.cityName+address.countyName+address.detailInfo //把地址拼接起来
    let goods=[]  // 获取我们需要的数组
    this.data.cartList.forEach(item=>{
      goods.push({
        goods_id:item.id,
        goods_number:item.num,
        goods_price:item.price/item.num
      })
    })
    let obj={
      order_price:this.data.priceAll,
      consignee_addr:addr,
      goods:goods
    }
    let res=await createOrder(obj)
    let pres=await payOrder({order_number:res.message.order_number})
      wx.requestPayment({  // 发起支付
        nonceStr: pres.message.pay.nonceStr,
        package: pres.message.pay.package,
        paySign: pres.message.pay.paySign,
        timeStamp: pres.message.pay.timeStamp,
        signType:pres.message.pay.signType,
      })
    let msg=payStatus({order_number:pres.message.order_number})
    console.log(msg);
     /* 删除提交对的商品 */
     let data=wx.getStorageSync('cartList')
     data=data.filter(item=>!item.checked)
     wx.setStorageSync('cartList', data)
    //  成功跳到订单
     wx.navigateTo({
       url: '/pages/order/order?type=1',
     });
  },
  computed(){
    let num=0
    let price=0
    this.data.cartList.forEach(item=>{
      if(item.checked){
        num+=item.num
        price+=item.num*item.price
      }
    })
    this.setData({
      priceAll:price,
      numAll:num
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let address=wx.getStorageSync('address') || ''
    let cartList=wx.getStorageSync('cartList') || []
    cartList=cartList.filter(item=>item.checked)
    this.setData({
      address:address,
      cartList:cartList
    })
    this.computed()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})